package com.czh.mybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czh.mybatisplus.entity.User;
import com.czh.mybatisplus.service.IUserService;
import com.czh.mybatisplus.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author czh
 * @since 2024-09-05
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    @ApiOperation(value = "用户数据")
    @PostMapping("all")
    public Result<Map<String,Object>> all(){
        List<User> list = userService.list();
        Map<String,Object> map = new HashMap<>();
        map.put("userList",list);
        return Result.success(map);
    }

    @ApiOperation(value = "save(Entity)",notes = "插入一条数据，User user:实体类对象")
    @PostMapping("save")
    public Result save(@RequestBody User user){
        boolean bool = userService.save(user);
        return Result.success(bool);
    }

    @ApiOperation(value = "saveBatch(List<Entity>)",notes = "批量插入数据，List<实体类> list：条数")
    @PostMapping("saveBatch")
    public Result saveBatch(@RequestBody List<User> list){
        boolean bool = userService.saveBatch(list);
        return Result.success(bool);
    }

    @ApiOperation(value = "saveBatch(List<Entity>,Integer)",notes = "分批次插入数据，List<实体类> list：条数，Integer number：批次")
    @PostMapping("saveBatch2")
    public Result saveBatch2(@RequestBody List<User> list,
                            @RequestParam(value = "number",required = false,defaultValue = "1") Integer number){
        boolean bool = userService.saveBatch(list,number);
        return Result.success(bool);
    }

    @ApiOperation(value = "saveOrUpdate(Entity)",notes = "根据符合 User对象的id 修改数据，结果为false=新增，User user：实体类对象")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody User user){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,user.getId());
        boolean bool = userService.saveOrUpdate(user,queryWrapper);
        return Result.success(bool);
    }

    @ApiOperation(value = "saveOrUpdate(Entity,Wrapper<T>)",notes = "根据 id 修改数据，结果为false=新增，User user：实体类对象，（条件）Integer id：实体类id，Wrapper<T> updateWrapper：条件构造")
    @PostMapping("saveOrUpdate2")
    public Result saveOrUpdate2(@RequestBody User user,
                               @RequestParam(value = "id",required = false,defaultValue = "0") Integer id){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,id);
        boolean bool = userService.saveOrUpdate(user,queryWrapper);
        return Result.success(bool);
    }

    @ApiOperation(value = "saveOrUpdateBatch(List<Entity>)",notes = "根据 符合id的User对象 修改数据，结果为false=新增，List<实体类> list：条数")
    @PostMapping("saveOrUpdateBatch")
    public Result saveOrUpdateBatch(@RequestBody List<User> list){
        boolean bool = userService.saveOrUpdateBatch(list);
        return Result.success(bool);
    }

    @ApiOperation(value = "saveOrUpdateBatch(List<Entity>,Integer)",notes = "修改（批量）结果为false=新增，List<实体类> list：条数，Integer number：批次")
    @PostMapping("saveOrUpdateBatch2")
    public Result saveOrUpdateBatch2(@RequestBody List<User> list,
                                     @RequestParam(value = "number",required = false,defaultValue = "1") Integer number){
        boolean bool = userService.saveOrUpdateBatch(list,number);
        return Result.success(bool);
    }

    @ApiOperation(value = "update(Entity,Wrapper<T>)",notes = "根据 User对象 修改数据，User user：实体类对象，Wrapper<T> updateWrapper：条件构造")
    @PostMapping("update")
    public Result update(@RequestBody User user){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,user.getId());
        boolean bool = userService.update(user,queryWrapper);
        return Result.success(bool);
    }

    @ApiOperation(value = "updateById(Entity)",notes = "根据 ID 修改数据，User user：实体类对象")
    @PostMapping("updateById")
    public Result updateById(@RequestBody User user){
        boolean bool = userService.updateById(user);
        return Result.success(bool);
    }

    @ApiOperation(value = "updateBatchById(List<Entity>)",notes = "根据ID 批量修改数据，List<实体类> list：条数")
    @PostMapping("updateBatchById")
    public Result updateBatchById(@RequestBody List<User> list){
        boolean bool = userService.updateBatchById(list);
        return Result.success(bool);
    }

    @ApiOperation(value = "updateBatchById(List<Entity>,Integer)",notes = "根据ID 分批次修改数据，List<实体类> list：条数，Integer number：批次")
    @PostMapping("updateBatchById2")
    public Result updateBatchById2(@RequestBody List<User> list,
                                  @RequestParam(value = "number",required = false,defaultValue = "1") Integer number){
        boolean bool = userService.updateBatchById(list,number);
        return Result.success(bool);
    }

    @ApiOperation(value = "remove(Wrapper<T>)",notes = "按条件删除数据，（条件）Integer id：删除的id")
    @PostMapping("remove")
    public Result remove(@RequestParam(value = "id",required = false,defaultValue = "0")Integer id){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,id);
        boolean bool = userService.remove(queryWrapper);
        return Result.success(bool);
    }

    @ApiOperation(value = "removeById(Integer)",notes = "根据 id 删除数据，（条件）Integer id：实体类id")
    @PostMapping("removeById")
    public Result removeById(@RequestParam(value = "id",required = false,defaultValue = "0")Integer id){
        boolean bool = userService.removeById(id);
        return Result.success(bool);
    }

    @ApiOperation(value = "removeByIds(List<Integer>)",notes = "根据 ID 批量删除，List<Integer> listId：实体类id")
    @PostMapping("removeByIds")
    public Result removeByIds(@RequestBody List<Integer> listId){
        boolean bool = userService.removeByIds(listId);
        return Result.success(bool);
    }

    @ApiOperation(value = "removeByMap(Map<String,Object>)",notes = "根据符合 Map<字段名,值> 条件，删除记录")
    @PostMapping("removeByMap")
    public Result removeByMap(@RequestBody Map<String, Object> map){
        boolean bool = userService.removeByMap(map);
        return Result.success(bool);
    }

    @ApiOperation(value = "getById(Integer)",notes = "根据 ID 查询，Integer id：实体类id")
    @PostMapping("getById")
    public Result getById(@RequestParam(value = "id",required = false,defaultValue = "1") Integer id){
        User user = userService.getById(id);
        Map<String,User> map = new HashMap<>();
        map.put("user",user);
        return Result.success(map);
    }

    @ApiOperation(value = "getOne(Wrapper<T>)",notes = "根据条件查询一条记录，（条件）String name：用户名，Wrapper<T> queryWrapper：条件构造")
    @PostMapping("getOne")
    public Result getOne(@RequestParam(value = "name",required = false,defaultValue = "张三") String name){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getName,name);
        User user = userService.getOne(queryWrapper);
        Map<String,User> map = new HashMap<>();
        map.put("user",user);
        return Result.success(map);
    }

    @ApiOperation(value = "getOne(Wrapper<T>,Boolean)",notes = "根据条件查询一条记录， boolean throwEx：如果是多个数据会抛出异常（true/是），（条件）String name：按用户名查询，Wrapper<T> queryWrapper：条件构造")
    @PostMapping("getOne2")
    public Result getOne2(@RequestParam(value = "throwEx",required = false,defaultValue = "false") boolean throwEx,
                         @RequestParam(value = "name",required = false,defaultValue = "张三") String name){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getName,name);
        User user = userService.getOne(queryWrapper,throwEx);
        Map<String,User> map = new HashMap<>();
        map.put("user",user);
        return Result.success(map);
    }

    @ApiOperation(value = "getMap(Wrapper<T>)",notes = "根据条件查询一条记录，（条件）String name：用户名，Wrapper<T> queryWrapper：条件构造")
    @PostMapping("getMap")
    public Result getMap(@RequestParam(value = "name",required = false,defaultValue = "张三") String name){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getName,name);
//        查询并返回一个Map
        Map<String, Object> map = userService.getMap(queryWrapper);
        return Result.success(map);
    }

    @ApiOperation(value = "getObj(Wrapper<T>,Function<?,V>)",notes = "根据条件查询一条记录并返回符合的单条记录的第一个字段值并强转指定类型，（条件）String name：按用户名查询，（必须）Wrapper<T> queryWrapper：条件构造")
    @PostMapping("getObj")
    public Result getObj(@RequestParam(value = "name",required = false,defaultValue = "张三") String name){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getName,name);
//        查询符合的单条记录的第一个字段值
        String str = userService.getObj(queryWrapper, (obj) -> String.valueOf(obj));
        Map<String,Object> map = new HashMap<>();
        map.put("id",str);
        return Result.success(map);
    }

    @ApiOperation(value = "list()",notes ="查询所有")
    @PostMapping("list")
    public Result<Map<String,Object>> list(){
        List<User> list = userService.list();
        Map<String,Object> map = new HashMap<>();
        map.put("user",list);
        return Result.success(map);
    }

    @ApiOperation(value = "list(Wrapper<T>)",notes ="查询列表(根据id查单条数据)，Wrapper<T> queryWrapper：条件构造")
    @PostMapping("list2")
    public Result<Map<String,Object>> list2(
            @RequestParam(value = "id",required = false,defaultValue = "1") Integer id){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.le(User::getId,id);
        List<User> list = userService.list(queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("user",list);
        return Result.success(map);
    }

    @ApiOperation(value = "listByIds(List<Integer>)",notes ="查询（根据List<Id> 批量查询）")
    @PostMapping("listByIds")
    public Result<Map<String,Object>> listByIds(@RequestBody List<Integer> idList){
        List<User> list = userService.listByIds(idList);
        Map<String,Object> map = new HashMap<>();
        map.put("user",list);
        return Result.success(map);
    }

    @ApiOperation(value = "listByMap(Map<String, Object>)",notes ="查询（根据 Map<字段名,值> 条件查询）")
    @PostMapping("listByMap")
    public Result<Map<String,Object>> listByMap(@RequestBody Map<String, Object> map){
        List<User> list = userService.listByMap(map);
        Map<String,Object> map2 = new HashMap<>();
        map2.put("user",list);
        return Result.success(map2);
    }

    @ApiOperation(value = "listMaps()",notes ="查询所有列表")
    @PostMapping("listMaps")
    public Result<List> listMaps(){
        List<Map<String, Object>> list = userService.listMaps();
        return Result.success(list);
    }

    @ApiOperation(value = "listMaps(Wrapper<T>)",notes ="按条件查询所有列表，String name：姓氏")
    @PostMapping("listMaps2")
    public Result<List> listMaps2(@RequestParam(value = "name",required = false,defaultValue = "张") String name){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(User::getName,name);
        List<Map<String, Object>> list = userService.listMaps(queryWrapper);
        return Result.success(list);
    }

    @ApiOperation(value = "listObjs()",notes ="查询全部id")
    @PostMapping("listObjs")
    public Result<Map<String,Object>> listObjs(){
        List<Object> objs = userService.listObjs();
        Map<String,Object> map = new HashMap<>();
        map.put("userId",objs);
        return Result.success(map);
    }

    @ApiOperation(value = "listObjs(Wrapper<T>)",notes ="根据条件查询全部id，String name：姓氏")
    @PostMapping("listObjs2")
    public Result<Map<String,Object>> listObjs2(@RequestParam(value = "name",required = false,defaultValue = "张") String name){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(User::getName,name);
        List<Object> objs = userService.listObjs(queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("userId",objs);
        return Result.success(map);
    }

    @ApiOperation(value = "listObjs(Function<?,V>)",notes ="查询全部id")
    @PostMapping("listObjs3")
    public Result<Map<String,Object>> listObjs3(){
        List<Object> objs = userService.listObjs(obj -> String.valueOf(obj));
        Map<String,Object> map = new HashMap<>();
        map.put("userId",objs);
        return Result.success(map);
    }

    @ApiOperation(value = "listObjs(Wrapper<T>,Function<?,V>)",notes ="根据条件查询符合的数据，String name：姓氏")
    @PostMapping("listObjs4")
    public Result<Map<String,Object>> listObjs4(@RequestParam(value = "name",required = false,defaultValue = "张") String name){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(User::getName,name);
        List<Object> objs = userService.listObjs(queryWrapper,obj -> String.valueOf(obj));
        Map<String,Object> map = new HashMap<>();
        map.put("userId",objs);
        return Result.success(map);
    }

    @ApiOperation(value = "page(IPage<T>)",notes ="无条件分页查询")
    @PostMapping("page")
    public Result<Map<String,Object>> page(){
        //一页，10条
        IPage<User> iPage = new Page<>(1,10);
        userService.page(iPage);
        Map<String,Object> map = new HashMap<>();
        map.put("user",iPage.getRecords());
        return Result.success(map);
    }

    @ApiOperation(value = "page(IPage<T>,Wrapper<T>)",notes ="条件分页查询")
    @PostMapping("page2")
    public Result<Map<String,Object>> page2(@RequestParam(value = "name",required = false,defaultValue = "小七") String name){
        //一页，10条
        IPage<User> iPage = new Page<>(1,10);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(User::getName,name);
        userService.page(iPage,queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("user",iPage.getRecords());
        return Result.success(map);
    }

    @ApiOperation(value = "pageMaps(IPage<T>)",notes ="无条件分页查询")
    @PostMapping("pageMaps")
    public Result<Page<Map<String,Object>>> pageMaps(){
        //一页，10条
        Page<Map<String, Object>> page = userService.pageMaps(new Page<>(1, 10));
        return Result.success(page);
    }

    @ApiOperation(value = "pageMaps(IPage<T>,Wrapper<T>)",notes ="条件分页查询")
    @PostMapping("pageMaps2")
    public Result<Page<Map<String,Object>>> pageMaps2(@RequestParam(value = "name",required = false,defaultValue = "小七") String name){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(User::getName,name);
        //一页，10条
        Page<Map<String, Object>> page = userService.pageMaps(new Page<>(1, 10),queryWrapper);
        return Result.success(page);
    }

    @ApiOperation(value = "count()",notes ="查询总记录数")
    @PostMapping("count")
    public Result<Map<String,Object>> count(){
        long count = userService.count();
        Map<String,Object> map = new HashMap<>();
        map.put("count",count);
        return Result.success(map);
    }

    @ApiOperation(value = "count(Wrapper<T>)",notes ="根据条件，查询总记录数")
    @PostMapping("count2")
    public Result<Map<String,Object>> count2(@RequestParam(value = "name",required = false,defaultValue = "小七") String name){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(User::getName,name);
        long count = userService.count(queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("count",count);
        return Result.success(map);
    }

}
